// pages/user/user-balance/index.js
const app = getApp()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        CustomBar: app.globalData.CustomBar,
        theme: app.globalData.theme, //全局颜色变量
        userInfo: {},
        rechargeList: [],
        curIndex: null,
        rechargeMoney: null,
        balanceConfig: {
            // 是否允许用户充值
            isEntrance: 0,
            // 是否允许自定义金额
            isCustom: 0,
            // 是否自动匹配套餐
            isMatchPlan: 0,
            // 充值说明
            directions: ''
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            theme: app.globalData.theme
        });
        this.userInfoGet()
        this.loadBalanceConfig()
    },

    // 获取商城用户信息
    userInfoGet() {
        app.api.userInfoGet()
            .then(res => {
                this.setData({
                    userInfo: res.data
                })
            })
    },
    
    rechargeClick(e) {
        this.setData({
            curIndex: e.currentTarget.dataset.index,
            rechargeMoney: null
        })
    },

    inputClick() {
        this.setData({
            curIndex: null
        })
    },

    userBalanceGoodsList() {
        app.api.balanceGoodsList()
            .then(res => {
                this.setData({
                    rechargeList: res.data
                })
            })
    },

    formSubmit: function (e) {
        const that = this
        // 提交订单
        let userBalanceGoodsId = ''
        let paymentPrice = e.detail.value.rechargeMoney
        if (this.data.curIndex != null) {
            // 套餐
            userBalanceGoodsId = this.data.rechargeList[this.data.curIndex].id
            paymentPrice = null
        } else if (!paymentPrice || paymentPrice === 0) {
            wx.showToast({
                title: '请输入充值金额',
                icon: 'none',
                duration: 3000
            })
            return
        }
        this.setData({
            loading: true
        })
        app.api.balanceOrderSave({
            userBalanceGoodsId: userBalanceGoodsId,
            paymentPrice: paymentPrice
        }).then(res => {
            app.api.unifiedBalanceOrder({
                id: res.data.id,
                tradeType: 'JSAPI'
            }).then(res2 => {
                this.setData({
                    loading: false
                })
                let payData = res2.data
                wx.requestPayment({
                    'timeStamp': payData.timeStamp,
                    'nonceStr': payData.nonceStr,
                    'package': payData.packageValue,
                    'signType': payData.signType,
                    'paySign': payData.paySign,
                    'success': function (res) {
                        that.paySuccess()
                    },
                    'fail': function (res) {
                    },
                    'complete': function (res) {
                        console.log(res)
                    }
                })
            })
        })
    },
    paySuccess: function () {
        this.userInfoGet()
        this.userBalanceGoodsList()
        this.setData({
            curIndex: null,
            rechargeMoney: null
        })
    },
    loadBalanceConfig: function () {
        app.api.balanceConfigGet()
            .then(res => {
                let data = res.data
                if (data) {
                    if (data.isEntrance && data.isEntrance === '1') {
                        this.userBalanceGoodsList()
                    }
                    this.setData({
                        balanceConfig: data
                    })
                }
            })
    }
})